
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.*;

import javax.media.protocol.*;
import javax.media.format.*;
import java.io.File;
import java.io.IOException;


public class RTPtransmitter{
	private MediaLocator mediaLocator = null;
	private DataSink dataSink = null;
	private Processor mediaProcessor = null;
	
	private static final Format[] FORMATS = new Format[]{
		new AudioFormat(AudioFormat.GSM_RTP)
		};
	
	private static final ContentDescriptor CONTENT_DESCRIPTOR =
		new ContentDescriptor(ContentDescriptor.RAW_RTP);
	
	public RTPtransmitter(String outIP, int outPort){
		String location = "rtp://" + outIP + ":" + outPort + "/audio";
		mediaLocator = new MediaLocator(location);
	}
	
	public void startTransmitting() throws IOException {
		mediaProcessor.start();
		dataSink.open();
		dataSink.start();
	}

	public void stopTransmitting(){
		try {
			dataSink.stop();
			dataSink.close();
			mediaProcessor.stop();
			mediaProcessor.close();
			System.out.println("stopped...");
		} catch (IOException ex) {
			Logger.getLogger(RTPtransmitter.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
	
	public void setDataSource(DataSource ds) throws IOException,
	NoProcessorException, CannotRealizeException, NoDataSinkException {


		mediaProcessor = Manager.createRealizedProcessor(
				new ProcessorModel(ds, FORMATS, CONTENT_DESCRIPTOR));


		dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(),
				mediaLocator);
	}
	
	public static void main(String [] args) {
		
		DataSource source = null;

		try {
			
			RTPtransmitter transmitter = new RTPtransmitter("192.168.0.105", 49184);
			System.out.println("-> Created media locator");
			

			File mediaFile = new File("test.wav");
			
			source = Manager.createDataSource(new MediaLocator(mediaFile.toURI().toURL()));


			transmitter.setDataSource(source);
			System.out.println("-> Set the data source on the transmitter");

			transmitter.startTransmitting();
			System.out.println("-> Transmitting...");
			
			try {
			    Thread.currentThread();
				Thread.sleep(10000);
			} catch (InterruptedException ie) {
			}
			System.out.println("-> Stopping...");
			transmitter.stopTransmitting();

		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
}